home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Kant Generator Pro 1.2 / src / Shell ƒ / window layer.c < prev    next >
Text File  |  1995-02-07  |  8KB  |  330 lines

  1. #define USE_FLOATING_WINDOWS    0        /* set to 1 for window layers */
  2.  
  3. #include "window layer.h"
  4. #include "util.h"
  5.  
  6. static    WindowPtr MyNewWindowDispatch(short layer, Boolean useColor,
  7.                 ExtendedWindowPtr storage, const Rect *boundsRect, ConstStr255Param title,
  8.                 short procID, Boolean goAwayFlag, long refCon);
  9. static    void SetFrontWindowInLayer(WindowPtr newWindow, WindowPtr oldFrontWindow,
  10.                 short layer);
  11. static    WindowPtr GetFrontWindowInLayer(short layer);
  12. static    void SetWindowPrevious(WindowPtr window, WindowPtr previousWindow);
  13. static    WindowPtr GetWindowPrevious(WindowPtr window);
  14. static    void SetWindowNext(WindowPtr window, WindowPtr nextWindow);
  15. static    WindowPtr GetWindowNext(WindowPtr window);
  16. static    void SetWindowLayer(WindowPtr window, short layer);
  17. static    short GetWindowLayer(WindowPtr window);
  18. static    pascal void MyHiliteWindow(WindowPtr theWindow, Boolean fHilite);
  19. static    void ActivateWindow(WindowPtr window, Boolean activate);
  20.  
  21. enum    /* window layers */
  22. {
  23.     kFloatLayer=0, kDocumentLayer
  24. };
  25.  
  26. #define NUM_LAYERS        2
  27. #define kMagicNumber    0x16435934
  28.  
  29. static    WindowPtr            gFrontWindowInLayer[NUM_LAYERS];
  30. static    UniversalProcPtr    gOldHiliteRoutine;
  31.  
  32. Boolean            gIgnoreNextActivateEvent;
  33.  
  34. void InitTheWindowLayer(void)
  35. {
  36. #if USE_FLOATING_WINDOWS
  37.     short            i;
  38.     
  39.     for (i=0; i<NUM_LAYERS; i++)
  40.         gFrontWindowInLayer[i]=0L;
  41.     
  42.     InstallHilitePatch();
  43. #endif
  44.     gIgnoreNextActivateEvent=FALSE;
  45. }
  46.  
  47. void ShutDownTheWindowLayer(void)
  48. {
  49. // well, no
  50. }
  51.  
  52. WindowPtr MyNewWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title,
  53.     Boolean visible, short procID, WindowPtr behind, Boolean goAwayFlag, long refCon)
  54. {
  55.     return MyNewWindowDispatch(kDocumentLayer, FALSE, (ExtendedWindowPtr)wStorage, boundsRect,
  56.         title, procID, goAwayFlag, refCon);
  57. }
  58.  
  59. WindowPtr MyNewCWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title,
  60.     Boolean visible, short procID, WindowPtr behind, Boolean goAwayFlag, long refCon)
  61. {
  62.     return MyNewWindowDispatch(kDocumentLayer, TRUE, (ExtendedWindowPtr)wStorage, boundsRect,
  63.         title, procID, goAwayFlag, refCon);
  64. }
  65.  
  66. WindowPtr MyNewFloatWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title,
  67.     Boolean visible, short procID, WindowPtr behind, Boolean goAwayFlag, long refCon)
  68. {
  69.     return MyNewWindowDispatch(kFloatLayer, FALSE, (ExtendedWindowPtr)wStorage, boundsRect,
  70.         title, procID, goAwayFlag, refCon);
  71. }
  72.  
  73. WindowPtr MyNewFloatCWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title,
  74.     Boolean visible, short procID, WindowPtr behind, Boolean goAwayFlag, long refCon)
  75. {
  76.     return MyNewWindowDispatch(kFloatLayer, TRUE, (ExtendedWindowPtr)wStorage, boundsRect,
  77.         title, procID, goAwayFlag, refCon);
  78. }
  79.  
  80. WindowPtr MyNewWindowDispatch(short layer, Boolean useColor,
  81.     ExtendedWindowPtr storage, const Rect *boundsRect, ConstStr255Param title, short procID,
  82.     Boolean goAwayFlag, long refCon)
  83. {
  84.     WindowPtr        window;
  85.     WindowPtr        oldFrontWindow;
  86.     WindowPtr        behind;
  87. #if USE_FLOATING_WINDOWS
  88.     WindowPtr        iter, nextIter;
  89. #endif
  90.         
  91.     behind=(WindowPtr)-1L;
  92. #if USE_FLOATING_WINDOWS
  93.     if (layer==kDocumentLayer)
  94.     {
  95.         iter=GetFrontWindowInLayer(kFloatLayer);
  96.         if (iter!=0L)
  97.         {
  98.             while ((nextIter=GetWindowNext(iter))!=0L)
  99.                 iter=nextIter;
  100.             behind=iter;
  101.         }
  102.     }
  103. #else
  104.     layer=kDocumentLayer;
  105. #endif
  106.     
  107.     window=useColor ?
  108.             NewCWindow(storage, boundsRect, title, TRUE, procID, behind, goAwayFlag, refCon) :
  109.             NewWindow(storage, boundsRect, title, TRUE, procID, behind, goAwayFlag, refCon);
  110.     
  111.     oldFrontWindow=GetFrontWindowInLayer(layer);
  112.     
  113. #if USE_FLOATING_WINDOWS
  114.     RemoveHilitePatch();
  115.     if ((layer==kDocumentLayer) && (oldFrontWindow!=0L))
  116.         HiliteWindow(oldFrontWindow, FALSE);
  117.     HiliteWindow(window, TRUE);
  118.     InstallHilitePatch();
  119.     
  120.     if ((layer==kFloatLayer) && (GetFrontDocumentWindow()!=0L))
  121.         gIgnoreNextActivateEvent=TRUE;
  122. #endif
  123.     
  124.     storage->magic=kMagicNumber;        /* so we know it's one of ours */
  125.     SetWindowLayer(window, layer);
  126.     
  127.     SetFrontWindowInLayer(window, oldFrontWindow, layer);
  128.     
  129.     return window;
  130. }
  131.  
  132. void MyDisposeWindow(WindowPtr window)
  133. {
  134.     WindowPtr        newFrontWindow;
  135.     short            layer;
  136.     
  137.     if (WindowHasLayer(window))
  138.     {
  139.         layer=GetWindowLayer(window);
  140.         newFrontWindow=GetWindowNext(window);
  141.         if (newFrontWindow!=0L)
  142.             SetWindowPrevious(newFrontWindow, 0L);
  143.         gFrontWindowInLayer[layer]=newFrontWindow;
  144.         CloseWindow(window);
  145.         DisposePtr((Ptr)window);
  146.         window=0L;
  147. #if USE_FLOATING_WINDOWS
  148.         if (newFrontWindow!=0L)
  149.         {
  150.             RemoveHilitePatch();
  151.             HiliteWindow(newFrontWindow, TRUE);
  152.             InstallHilitePatch();
  153.         }
  154. #endif
  155.     }
  156.     else
  157.     {
  158.         DisposeWindow(window);
  159.     }
  160. }
  161.  
  162. Boolean MySelectWindow(WindowPtr window)
  163. {
  164.     WindowPtr        nextWindow, previousWindow, oldFrontWindow;
  165.     short            layer;
  166. #if USE_FLOATING_WINDOWS
  167.     WindowPtr        iter, nextIter;
  168. #endif
  169.     
  170.     if (WindowHasLayer(window))
  171.     {
  172.         layer=GetWindowLayer(window);
  173.         oldFrontWindow=GetFrontWindowInLayer(layer);
  174.         if (oldFrontWindow!=window)
  175.         {
  176.             nextWindow=GetWindowNext(window);            /* might be 0L */
  177.             previousWindow=GetWindowPrevious(window);    /* guaranteed !=0L */
  178.             SetWindowNext(previousWindow, nextWindow);
  179.             if (nextWindow!=0L)
  180.                 SetWindowPrevious(nextWindow, previousWindow);
  181.             SetFrontWindowInLayer(window, oldFrontWindow, layer);
  182.         }
  183.         
  184. #if USE_FLOATING_WINDOWS
  185.         if ((layer==kDocumentLayer) && ((iter=GetFrontWindowInLayer(kFloatLayer))!=0L))
  186.         {
  187.             while ((nextIter=GetWindowNext(iter))!=0L)
  188.                 iter=nextIter;
  189.             SendBehind(window, iter);
  190.         }
  191.         else
  192. #endif
  193.         {
  194.             SelectWindow(window);
  195.         }
  196.         
  197.         if (oldFrontWindow==window)
  198.             return FALSE;
  199.         
  200. #if USE_FLOATING_WINDOWS
  201.         RemoveHilitePatch();
  202.         if ((layer==kDocumentLayer) && (oldFrontWindow!=0L))
  203.         {
  204.             HiliteWindow(oldFrontWindow, FALSE);
  205.         }
  206.         HiliteWindow(window, TRUE);
  207.         InstallHilitePatch();
  208. #endif
  209.     }
  210.     else
  211.     {
  212.         SelectWindow(window);
  213.     }
  214.     
  215.     return TRUE;
  216. }
  217.  
  218. void SetFrontWindowInLayer(WindowPtr newWindow, WindowPtr oldFrontWindow,
  219.     short layer)
  220. {
  221.     if (newWindow!=0L)
  222.         SetWindowNext(newWindow, oldFrontWindow);
  223.     if (oldFrontWindow!=0L)
  224.         SetWindowPrevious(oldFrontWindow, newWindow);
  225.     if (newWindow!=0L)
  226.         SetWindowPrevious(newWindow, 0L);
  227.     gFrontWindowInLayer[layer]=newWindow;
  228. }
  229.  
  230. WindowPtr GetFrontWindowInLayer(short layer)
  231. {
  232.     return gFrontWindowInLayer[layer];
  233. }
  234.  
  235. void SetWindowPrevious(WindowPtr window, WindowPtr previousWindow)
  236. {
  237.     ((ExtendedWindowPtr)window)->previousWindow=previousWindow;
  238. }
  239.  
  240. WindowPtr GetWindowPrevious(WindowPtr window)
  241. {
  242.     return ((ExtendedWindowPtr)window)->previousWindow;
  243. }
  244.  
  245. void SetWindowNext(WindowPtr window, WindowPtr nextWindow)
  246. {
  247.     ((ExtendedWindowPtr)window)->nextWindow=nextWindow;
  248. }
  249.  
  250. WindowPtr GetWindowNext(WindowPtr window)
  251. {
  252.     return ((ExtendedWindowPtr)window)->nextWindow;
  253. }
  254.  
  255. void SetWindowLayer(WindowPtr window, short layer)
  256. {
  257.     ((ExtendedWindowPtr)window)->layer=layer;
  258. }
  259.  
  260. short GetWindowLayer(WindowPtr window)
  261. {
  262.     return ((ExtendedWindowPtr)window)->layer;
  263. }
  264.  
  265. Boolean WindowHasLayer(WindowPtr window)
  266. {
  267.     if (window==0L)
  268.         return FALSE;
  269.     else
  270.         return ((ExtendedWindowPtr)window)->magic==kMagicNumber;
  271. }
  272.  
  273. Boolean WindowIsFloat(WindowPtr window)
  274. {
  275. #if USE_FLOATING_WINDOWS
  276.     if (window!=0L)
  277.         return ((ExtendedWindowPtr)window)->layer==kFloatLayer;
  278.     else
  279. #endif
  280.         return FALSE;
  281. }
  282.  
  283. WindowPtr GetFrontDocumentWindow(void)
  284. {
  285. #if USE_FLOATING_WINDOWS
  286.     return GetFrontWindowInLayer(kDocumentLayer);
  287. #else
  288.     return FrontWindow();
  289. #endif
  290. }
  291.  
  292. WindowPtr GetIndWindowPtr(short index)
  293. {
  294.     WindowPtr        w;
  295.     short            i;
  296.     
  297.     for (i=NUM_LAYERS-1; i>=0; i--)
  298.     {
  299.         w=GetFrontWindowInLayer(i);
  300.         while ((w!=0L) && (GetWindowIndex(w)!=index))
  301.             w=GetWindowNext(w);
  302.         if (w!=0L)
  303.             return w;
  304.     }
  305.     
  306.     return 0L;
  307. }
  308.  
  309. void InstallHilitePatch(void)
  310. {
  311. #if USE_FLOATING_WINDOWS
  312.     UniversalProcPtr    newAddress;
  313.     
  314.     gOldHiliteRoutine=GetToolTrapAddress(_HiliteWindow);
  315.     newAddress=(UniversalProcPtr)StripAddress(MyHiliteWindow);
  316.     SetToolTrapAddress(newAddress, (short)_HiliteWindow);
  317. #endif
  318. }
  319.  
  320. void RemoveHilitePatch(void)
  321. {
  322. #if USE_FLOATING_WINDOWS
  323.     SetToolTrapAddress(gOldHiliteRoutine, (short)_HiliteWindow);
  324. #endif
  325. }
  326.  
  327. pascal void MyHiliteWindow(WindowPtr theWindow, Boolean fHilite)
  328. {
  329. }
  330.